package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.controlPlanNewPojo;
import com.example.pojo.controlPlanPojo;
import com.example.service.controlPlanNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:controlPlanNewController
 * Description:
 *新版控制计划页面
 * @Author 赵永峰
 * @Create 2025/6/27 16:34
 * @Version 1.0
 */
@RestController
public class controlPlanNewController {
    @Autowired
    controlPlanNewService controlPlanNewService;
    //对控制计划插入多行数据，前端来源是EXCEL插入的。
    @PostMapping("/insertMultiLineControlPlanControlPlanNew")
    public Result insertMultiLineControlPlanControlPlanNew(@RequestBody List<controlPlanNewPojo> pojos){
        controlPlanNewService.insertMultiLine(pojos);
        return Result.success("插入成功");
    }
    //辅助函数：根据两个字符串类型的字段，查询分别等于某一值的数据(控制计划的表）
    @PostMapping("/queryTwoFieldControlPlanControlPlanNew")
    public Result queryTwoFieldControlPlanControlPlanNew(@RequestBody Map<String,Object> map){
        List<controlPlanNewPojo> pojos= controlPlanNewService.queryTwoField(map);
        return Result.success(pojos);
    }

    //根据ID，修改一行的值
    @PostMapping("/editControlPlanControlPlanNew")
    public Result EditControlPlanControlPlanNew(@RequestBody controlPlanNewPojo pojo){
       //通过id修改某一行
        controlPlanNewService.editOneById(pojo);
        //返回一个成功的 Result 对象，消息为“修改成功”
        return Result.success("修改成功");
    }

    //插入一行
    @PostMapping("/insertControlPlanControlPlanNew")
    public Result InsertControlPlanControlPlanNew(@RequestBody List<controlPlanNewPojo> pojo){
        //循环接收到的数据，id≠new是原数据，id=new的是新插入的
        for (int i = 0; i < pojo.size(); i++) {
            if (pojo.get(i).getId().equals("new")){
                controlPlanNewService.insertOne(pojo.get(i));
            }
            else {
                controlPlanNewService.editOneById(pojo.get(i));
            }
        }
        Map<String ,Object> map=new HashMap<>();
        map.put("firstFieldName","workshop");
        map.put("firstFieldVal",pojo.get(0).getWorkshop());
        map.put("secondFieldName","drawing");
        map.put("secondFieldVal",pojo.get(0).getDrawing());
        map.put("thirdFieldName","op");
        map.put("thirdFieldVal",pojo.get(0).getOp());
        //重新查询这一序的控制计划，再返回给前端
        List<controlPlanNewPojo> queryThreeField=controlPlanNewService.queryThreeField(map);
        return Result.success(queryThreeField);
    }

    /*更改后的新版控制计划数据库的函数*/
    //根据三个字段查询所有数据：现在用在了根据工序号、发动机图号、车间名称进行查询。利用的是模糊查询。
    @PostMapping("/queryThreeFieldControlPlanControlPlanNew")
    public Result queryThreeFieldControlPlanControlPlanNew(@RequestBody Map<String,Object> parameters){
        List<controlPlanNewPojo> controlPlanPojoList= controlPlanNewService.queryThreeField(parameters);
        return Result.success(controlPlanPojoList);
    }
}
